<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <button>11</button>
    <button>22</button>
    <button>33</button>

    <script>

        const oBtns = document.querySelectorAll('button');

        for (var i = 0; i < oBtns.length; i++) {
            // 利用了匿名函数的自调用    函数是为了生成作用域
            (function (i) {
                oBtns[i].onclick = function () {
                    console.log(i);
                }
            })(i)

        }


        var i = 0;
        console.log(i);



    </script>

</body>

</html>